using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEngine;
using UnityEngine.Windows;
using File = System.IO.File;

namespace QFramework
{
    public class ItemKitMenu
    {

        [MenuItem("QFramework/ItemKit/Create Code")]
        public static void CreateCode()
        {
            // 拿到项目里所有的ItemConfig
            var itemConfigs = AssetDatabase.FindAssets($"t:{nameof(Item)}")
                .Select(guid => AssetDatabase.GUIDToAssetPath(guid))
                .Select(assetPath => AssetDatabase.LoadAssetAtPath<Item>(assetPath))
                .ToArray();

            var filePath = "Assets\\QFramework\\ItemKit\\Examples\\Items.cs";

            var rootCode = new RootCode()
                .Using("UnityEngine")
                .Using("QFramework")
                .EmptyLine()
                .Namespace("QFramework.Example", ns =>
                {
                    ns.Class("Items", String.Empty, false, false, c =>
                    {
                        foreach (var itemConfig in itemConfigs)
                        {
                            c.Custom($"public static string {itemConfig.name} = \"{itemConfig.Key}\";");
                        }
                    });
                });

            //var fileWriter = File.CreateText(filePath);
            //var codeWriter = new FileCodeWriter(fileWriter);
            //rootCode.Gen(codeWriter);
            //fileWriter.Close();
            // 和下面的效果一样
            
            using var fileWriter = File.CreateText(filePath);
            var codeWriter = new FileCodeWriter(fileWriter);
            rootCode.Gen(codeWriter);
            
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }
    }
}
